package com.flymobi.admin;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.flymobi.fw.modules.SM;
import com.flymobi.fw.util.DateUtil;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Manager;
import com.flymobi.po.Sp;
import com.flymobi.util.WebContext;
import com.flymobi.vo.SmsTongjiSearchVo;
import com.flymobi.vo.TongjiVo;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Oct 21, 2008 11:16:42 AM
 * @version $Id: TongjiAction.java,v 1.1 2010/04/09 03:27:08 airsou Exp $
 */
public class TongjiAction extends WebAction {
	private String s;
	private String e;
	/**
	 * @return the s
	 */
	public String getS() {
		return s;
	}

	/**
	 * @param s the s to set
	 */
	public void setS(String s) {
		this.s = s;
	}

	/**
	 * @return the e
	 */
	public String getE() {
		return e;
	}

	/**
	 * @param e the e to set
	 */
	public void setE(String e) {
		this.e = e;
	}

	/**
	 * @return the searchparam
	 */
	public SmsTongjiSearchVo getSearchparam() {
		return searchparam;
	}

	/**
	 * @param searchparam the searchparam to set
	 */
	public void setSearchparam(SmsTongjiSearchVo searchparam) {
		this.searchparam = searchparam;
	}

	private SmsTongjiSearchVo searchparam;

	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public String webProcess(WebContext context, Manager manager) {
		if (searchparam == null) {
			searchparam = new SmsTongjiSearchVo();
		}
		if ("list".equals(method)) {
			if (!StringUtil.isEmpty(s)) {
				searchparam.setStart(DateUtil.parseStrDate(s,
						"yyyy-MM-dd"));
			}
			if (!StringUtil.isEmpty(e)) {
				searchparam.setEnd(DateUtil.parseStrDate(e,
						"yyyy-MM-dd"));
			}
			if (searchparam == null) {
				searchparam = new SmsTongjiSearchVo();
			}
			if (searchparam.getStart() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setStart(calendar.getTime());
			}
			if (searchparam.getEnd() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setEnd(calendar.getTime());
			}
			List<Sp> spList=SM.SpService.list(startRow,pagePerSize);
			context.put("spList",spList);
			
			List<TongjiVo> tjList=new ArrayList<TongjiVo>();
			if(searchparam.getSpId()==null||searchparam.getSpId()==-1){
				for(Sp sp:spList){
					TongjiVo tjVo=new TongjiVo();
					tjVo.setStart(searchparam.getStart());
					tjVo.setEnd(searchparam.getEnd());
					tjVo.setSpId(sp.getId());
					tjVo.setSpName(sp.getSpName());
					tjVo.setSpNumber(sp.getSpNumber()+sp.getSubSpNumber());
					tjList.add(tjVo);
				}
			}else{
				Sp sp=SM.SpService.get(searchparam.getSpId()+"");
				TongjiVo tjVo=new TongjiVo();
				tjVo.setStart(searchparam.getStart());
				tjVo.setEnd(searchparam.getEnd());
				tjVo.setSpId(sp.getId());
				tjVo.setSpName(sp.getSpName());
				tjVo.setSpNumber(sp.getSpNumber()+sp.getSubSpNumber());
				tjList.add(tjVo);
			}
			context.put("tjList",tjList);
			return "tj";
		}else {
			method = "list";
			return this.webProcess(context, manager);
		}
	}
}
